package io.gitee.xyuesheng.mysqlday03;

import java.sql.*;

//使用jdbc连接数据库
public class TestMysql {
    public static void main(String[] args) {
        //加载驱动

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        try {
            //创建连接
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/daily", "root", "123456");

            PreparedStatement statement = connection.prepareStatement(
                    "select id,uid,created,content from daily");

            //执行查询,得到一个结果集合
            ResultSet resultSet = statement.executeQuery();

            //遍历结果集
            while (resultSet.next()) {
                //取出当前行额所有数据
                System.out.println("id:" + resultSet.getLong("id"));
                System.out.println("uid:" + resultSet.getLong("uid"));
                System.out.println("created:" + resultSet.getDate("created"));
                System.out.println("content" + resultSet.getString("content"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
